﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 行走
/// </summary>
[CreateAssetMenu(menuName = "MyScript/Act/ControllableAct/Chracter1Act/WalkAct")]
public class MoveAct : ControllableAct
{
    [SerializeField] private GameTypeDefine.BUTTON_ID m_horizontal;

    [SerializeField] private GameTypeDefine.BUTTON_ID m_vertical;

    [SerializeField] private Vector3 m_moveVector3;

    [SerializeField] private Vector3 m_inputVector3;

    private MoveComponent m_moveComponent;

    public override ControllableObjectState Act(ButtonEventArgs args, GameObject gameObject, ControllableObjectState state)
    {
        float moveSpeed = ((IMovableState)state).GetMoveSpeed(gameObject);

        if (null == m_moveComponent)
        {
            m_moveComponent = gameObject.GetComponent<MoveComponent>();
            if (null == m_moveComponent)
            {
                Debug.Log("Miss MoveComponent");
                return ControllableStateMachine.remainState;
            }
        }

        if (args.GetButtonID() == m_horizontal)
        {
            m_inputVector3.x = args.Axis;
            Move(moveSpeed);
        }

        if (args.GetButtonID() == m_vertical)
        {
            m_inputVector3.z = args.Axis;
            Move(moveSpeed);
        }

        return base.Act(args, gameObject, state);
    }

    private void Move(float moveSpeed)
    {
        float input = Mathf.Abs(m_inputVector3.x) + Mathf.Abs(m_inputVector3.z);
        if (input < 1)
        {
            moveSpeed = input * moveSpeed;
        }

        m_moveVector3 = moveSpeed * m_inputVector3.normalized;
        MyMath.CameraTransformDirection(CameraMgr.ActiveTransform.forward, ref m_moveVector3);

        m_moveComponent.Move(m_moveVector3);
    }
}
